from playwright.sync_api import expect, Page, Playwright,sync_playwright


class LoginPage:

    def __init__(self, page: Page):
        # 将传入的 `page` 对象赋值给类的实例属性 `self.page`，以便后续使用
        self.page = page
        self.login_iframe = page.frame_locator('#popup_login_frame')
        # 登录窗口iframe中的元素(登录弹窗是个iframe)
        self.locator_username = self.login_iframe.locator('#username')
        self.locator_password = self.login_iframe.locator('#j-password')
        self.locator_login_button = self.login_iframe.locator('.ptlogin_btn')
        self.locator_register_button = self.login_iframe.locator('#login_simple_toregister')
        self.locator_autologin = self.login_iframe.locator('#login_autoLogin')
        self.locator_forget_password = self.login_iframe.locator('#login_forgetPasswd')
        self.locator_problem_feedback = self.login_iframe.locator('.login_feedback')

        # 输入用户名或密码有误时报的错
        self.locator_username_error_tip = self.login_iframe.locator('#Msg').nth(0)

    def navigate(self):
        # 页面
        self.page.goto('/')

    def fill_username(self, username):
        self.locator_username.fill(username)

    def fill_password(self, password):
        self.locator_password.fill(password)

    def click_login_button(self):
        self.locator_login_button.click()

    def click_register_button(self):
        self.locator_register_button.click()

    def autologin_check(self):
        self.locator_autologin.check()

    def autologin_uncheck(self):
        self.locator_autologin.uncheck()

    def click_forget_password(self):
        self.locator_forget_password.click()

    def click_problem_feedback(self):
        self.locator_problem_feedback.click()

    def login(self,username,password):
        self.locator_username.fill(username)
        self.locator_password.fill(password)
        self.locator_login_button.click()